<?php

namespace Create\Controller;

use Create\Api\Exec;
use Create\Api\File;
use Think\Controller;

class IndexController extends Controller
{

    private $post = array(); //接受到的POST数据

    public function index()
    {
        //查询出当前项目的模块
        $file = new File();
        $models = $file->get_dir();
        if (empty($models)) {
            exit('请先创建好一个空模块');
        }
        $this->assign('models', $models);
        $this->display();
    }

    public function action()
    {
        $this->getPost();//接受post数据
        //需要执行的类名
        $data = $this->post ;
        $execArr = array();
        $array = array('databases','controller', 'view', 'model');
        foreach($data as $k=>$v){
            if(in_array($k,$array)){
                array_push($execArr,$k);
            }
        }
        //反转数组，数据库必须要首先创建
        $exec = array_reverse($execArr);
        $create = new Exec($exec);
        $create->table_name = $this->post['table_name'];
        $create->table_comment = $this->post['table_comment'];
        $create->models = $this->post['models'];
        $create->data = $this->post['rows'];
        $create->engine = $this->post['engine'] ;
        $create->create();
        echo 'ok';
    }

    /**
     * 在页面上点击添加一个字段的时候异步返还一个字段填写表单
     */
    public function addrow()
    {
        $this->display('rows');
    }


    /**
     * 接受POST数据
     * @return array
     */
    private function getPost()
    {
        $post = I('post.');
        $data = array('rows' => array());
        foreach ($post as $k => $v) {
            if (is_array($v)) {
                foreach ($v as $key => $c) {
                    $data['rows'][$key][$k] = $c;
                }
            } else {
                $data[$k] = $v;
            }
        }
        $this->post = array_reverse($data);
        return $this->post;
    }




}
